<?php
/**
 * 《报价管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Quote.class.php 1043 2010-04-01 08:04:38Z sunxy $
 */
require_once(dirname(__FILE__) . '/QuoteUtil.class.php');

class Quote extends QuoteUtil {
	public $name = '报价信息';
	public $tbl = Constant::tbl_quote;

	public $urgent = array(
		'-' => '请选择',
		'1' => '紧急',
		'2' => '重要',
		'3' => '普通',
	
		'9' => '其它',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待报价',

		'3' => '正在报价',
	// '4' => '重新报价',

		'5' => '暂停报价',
		'6' => '取消报价',

		'8' => '完成报价',
		'9' => '产品下单',
	);

	public $groupby = array(
		'-' => '请选择',
		'urgent' => '紧急程度',
		'emp' => '业务员',
		'bj' => '报价员',
		'times' => '次数',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	public $orderby = array(
		'-' => '请选择',
		'num' => '套数',
		'urgent' => '紧急程序',
		'date' => '要求日期',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('type', json_encode($this->type));

		$controller->assign_by_ref('urgent_option', $this->urgent);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'报价信息' => array('cc'=>'customer.php?do=Quote.find', 'js'=>'1'),
			'报价明细' => array('cc'=>'customer.php?do=QuoteItem.find', 'js'=>''),
			'附件信息' => array('cc'=>'customer.php?do=QuoteInfo.find', 'js'=>''),
			
			'我的客户' => array('cc'=>'contact.php?do=CustomerMy.find', 'js'=>'1'),
			'我的产品' => array('cc'=>'mold.php?do=MoldMy.find', 'js'=>'1'),
				
			'相关资料' => array('cc'=>'customer.php?do=HelpMy.layout', 'js'=>'1'),
			'资料管理' => array('cc'=>'customer.php?do=Help.find', 'js'=>'1'),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 year'));
		
		$ab = array(
			'-' => '请选择',
			'1' => '格式1',		
			'2' => '格式2',
			'3' => '格式3',
		);
		$controller->assign_by_ref('ab_option', $ab);
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '业务员',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'customer/quote/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$code_s = $request['code_s'];
		$category_s = $request['category_s'];
		$urgent_s = $request['urgent_s'];
		
		$bj_s = $this->_input($request['bj_s']);

		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		}

		$my = $this->_my();
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($code_s)) {
			$where .= " AND code = '" . $code_s . "'";
		}

		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($urgent_s) && $urgent_s <> '-') {
			$where .= " AND urgent = '" . $urgent_s . "'";
		}

		if (!empty($bj_s) && $bj_s <> '-') {
			$where .= " AND bj = '" . $bj_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['num_'] = $this->_num($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->assign('emp', $_SESSION['id']);
		$controller->assign('empname', $_SESSION['name']);

		$controller->display($request, 'customer/quote/list');
		return true;
	}

	function _num($id) {
		$ret = $this->_quote_item($id);
		
		return count($ret);
	}
	
	function _quote_item($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/QuoteItem.class.php');
		$aa = new QuoteItem();
		
		return $aa->_list($id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		require_once(dirname(__FILE__) . '/../contact/Customer.class.php');
		$customer = new Customer();
		$controller->assign_by_ref('customer', $customer->_view($id));

		$controller->assign('t1', strtotime('1 day'));

		$controller->display($request, 'customer/quote/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$code = $request['code'];
		$num = $request['num'];

		$category = $request['category'];
		$urgent = $request['urgent'];
		$date = $request['date'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			$ret = array('status'=> 2);

			echo json_encode($ret);
			return true;
		}
		
		require_once(dirname(__FILE__) . '/../contact/Customer.class.php');
		$aa = new Customer();
		
		$cc = $aa->_view_code($code);
		
		$bj = $cc['bj'];
		$customer = $cc['id'];
		
		$emp = $_SESSION['id'];

		$fmt = "INSERT INTO %s(customer, code, num, category, urgent, date, content, bj, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $customer, $code, $num, $category, $urgent, $date, $content, $bj, $emp, $emp);
		debug($sql);
		$db->query($sql);

		// $this->findPost($controller, $request);
		$ret = array('status'=> 1);

		echo json_encode($ret);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		if ($row['status'] == '8') {
			return $this->view($controller, $request);
		}

		$controller->assign_by_ref('result', $row);

		require_once(dirname(__FILE__) . '/../contact/Customer.class.php');
		$customer = new Customer();
		$controller->assign_by_ref('customer', $customer->_view($row['customer']));

		$controller->display($request, 'customer/quote/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$code = $request['code'];
		$num = $request['num'];
		
		$amount = str_replace(",", "", $request['amount']);
		
		$currency = $request['currency'];				
		$rate = $request['rate'];		
		$rmb = $request['rmb'];

		$category = $request['category'];
		$urgent = $request['urgent'];
		$date = $request['date'];
		$content = addslashes($request['content']);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET code = '%s', num = '%s', amount = '%s', currency = '%s', rate = '%s', rmb = '%s', category = '%s', urgent = '%s', date = '%s', content = '%s'";
		$fmt .= ", modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $num, $amount, $currency, $rate, $rmb, $category, $urgent, $date, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function item($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['item'] = json_encode($this->_quote_item($row['id']));
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/quote/item');
		return true;
	}
		
	function itemPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$code = $request['code'];
		$num = $request['num'];

		$category = $request['category'];
		
		$urgent = $request['urgent'];
		$date = $request['date'];
		
		$currency = $request['currency'];
		$amount = $request['amount'];
		$rate = $request['rate'];
		$rmb = $request['rmb'];
		
		$name = addslashes($request['name']);
		
		$content = addslashes($request['content']);

		$status = $request['status'];
		
		$this->_update_quote_item($request);
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET code = '%s', num = '%s', category = '%s', urgent = '%s', date = '%s', currency = '%s', amount = '%s'";
		$fmt .= ", content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $num, $category, $urgent, $date, $currency, $amount, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_quote_item($request) {
		require_once(dirname(__FILE__) . '/QuoteItem.class.php');
		$aa = new QuoteItem();
		
		return $aa->_update($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row['item'] = $this->_quote_item($row['id']);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/quote/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['type_t'] = $this->type[$row['type']];
		
		$row['exchange'] = $this->_exchange($row['currency']);
		
		$row['customer_t'] = Name::customer_code($row['customer']);
		$row['customer_x'] = json_decode($row['customer_x']);

		$row['category_t'] = $this->category[$row['category']];
		$row['urgent_t'] = $this->urgent[$row['urgent']];

		$row['bj_t'] = Name::emp($row['bj']);

		$row = $this->_view_t($row);
		
		return $row;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reason($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$controller->assign_by_ref('result', $this->_view($id));

		$controller->display($request, 'customer/quote/reason');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reasonPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$reason = addslashes($request['reason']);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET reason = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $reason, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$bj = $request['bj'];
		if (empty($emp)) {
			$bj = $_SESSION['id'];
		}

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET bj = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $bj, $status, $id);
		// debug($sql);
		$db->query($sql);
		
		if ($status == '3') {
			require_once(dirname(__FILE__) . '/QuoteInfo.class.php');
			$aa = new QuoteInfo();
			$aa->_update_bj($id, $bj);
		}

		$this->_notice_x($controller, $request, $id);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function status_continue($controller, $request) {
		$this->pretreat($controller);

		return $this->status_restart($controller, $request);
	}

	function status_restart($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$row = $this->id($this->tbl, $id);
		if (empty($row['emp'])) {
			$status = 2;
		} else {
			$status = 3;
		}

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 指定报价员
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function bj($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$controller->assign_by_ref('result', $this->_view($id));

		$controller->assign_by_ref('emp_option', $this->emp_dept('scbj'));

		$controller->display($request, 'customer/quote/bj');
		return true;
	}

	function bjPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$bj = $request['bj'];
		if (empty($bj)) {
			$bj = $_SESSION['id'];
		}

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET bj = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $bj, $id);
		// debug($sql);
		$db->query($sql);
		
		require_once(dirname(__FILE__) . '/QuoteInfo.class.php');
		$aa = new QuoteInfo();
		$aa->_update_bj($id, $bj);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			if ($this->exist_info($id)) {
				continue;
			}

			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, Constant::tbl_quote, $id);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$num = array(
			'1' => '按项目',
			'2' => '按产品',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'customer/quote/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(num) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(num) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} elseif ($px == 'urgent') {
			$row['a'] = $this->urgent[$row['a']];
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_code($controller, $request) {

		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request, $my = '1') {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		// $key = sprintf("%s_code_%s", $this->tbl, $q);
		if ($my == '2') {
			$key = sprintf("%s_%s_%s_%s", $this->tbl, "code_my", $_SESSION['id'], $q);
		} else {
			$key = sprintf("%s_%s_%s", $this->tbl, "code_all", $q);
		}
		
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code LIKE '%s' ORDER BY id DESC LIMIT 20";
		$sql = sprintf($fmt, $this->tbl, "%" . $q . "%");
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$ret[] = $row;
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}
	
	function ajax_my($controller, $request) {
		$data = $this->_ajax_code($request, '2');
		echo json_encode($data);
	}

	function ajax_a($controller, $request) {
		$id = $_COOKIE['id_'];
		$data = array();
		if (!$this->ajax_a_($id)) {
			$data['status'] = 2;
		} else {
			$data['status'] = 1;
		}

		$controller->assign('result', json_encode($data));

		$request['tpl'] = 'json';
		$controller->display($request, 'json');
		return true;
	}

	function ajax_a_($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['emp'] == $_SESSION['id']) {
				return true;
			}

			if ($row['bj'] == $_SESSION['id']) {
				return true;
			}
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_exist($controller, $request) {
		$code = $request['code'];

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code);
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			echo "false";
		} else {
			echo "true";
		}

		return true;
	}

	function ajax_bj($controller, $request) {
		$my = $_SESSION['emp'];
		if ($my['dept'] == 'scbj' || $this->_is_admin()) {
			$data['status'] = 1;
		} else {
			$data['status'] = 2;
		}

		echo json_encode($data);
		return true;
	}

	function _customer($mold) {
		$pos = strpos($mold, ".");

		$code = substr($mold, 0, $pos);
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, Constant::tbl_customer, $code);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['id'];
	}

	function ajax_reason($controller, $request) {
		$id = $_COOKIE['id_'];

		if ($this->_ajax_reason($id)) {
			$ret = array('status'=> 1);
		} else {
			$ret = array('status'=> 2);
		}

		echo json_encode($ret);
		return true;
	}

	function _ajax_reason($id) {
		if ($this->_is_admin()) {
			return true;
		}
		
		$id = $_COOKIE['id_'];
		$ret = array();
		
		if ($this->_ajax_x($id, 'emp')) {
			return true;
		} else if ($this->_ajax_x($id, 'oper')) {
			return true;
		} else if ($this->_ajax_x($id, 'bj')) {
			return true;
		}

		return false;
	}

	function t($controller, $request) {
		$db = newdb($this->dsn);

		$s = "3708.6";
		$c = preg_split("/[\.,]+/", $s);

		debug(print_r($c, true));

		logdate($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function z($controller, $request) {
		$db = newdb($this->dsn);

		$fmt = "SELECT code, count(code) t FROM %s GROUP BY code ORDER BY t DESC";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($row['t'] < 2) {
				continue;
			}

			$this->_update_info($row['code']);
		}

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}

	function _update_info($code) {
		$db = newdb($this->dsn);

		$fmt = "SELECT * FROM %s WHERE code = '%s' ORDER BY ID DESC";
		$sql = sprintf($fmt, $this->tbl, $code);
		echo $sql . "\n";
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		$quote = $row['id'];
		while ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET quote = '%s' WHERE quote = '%s'";
			$sql = sprintf($fmt, Constant::tbl_quote_info, $quote, $row['id']);
			echo $sql . "\n";
			$db->query($sql);

			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
	}

	/**
	 * 定时运行，每小时运行一次
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function report_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		require_once 'Plan.class.php';
		$plan = new Plan();

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status > 2 AND status < 6 AND createtime > '2010-02-01'";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$request['id'] = $row['id'];

			$plan->report_daily($controller, $request);
		}

		logdate($controller, $request);
		return true;
	}

	function _notice_x($controller, $request, $id) {
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		if ($row['status'] == '2') {
			$this->_notice_2($controller, $request, $row);
		}

		if ($row['status'] == '3') {
			$this->_notice_3($controller, $request, $row);
		}

		if ($row['status'] == '8') {
			$this->_notice_8($controller, $request, $row);
		}
	}

	function _notice_quote($data) {
		$ret = array();

		if (!empty($data['emp'])) {
			$ret[] = $data['emp'];
			return $ret;
		}
		
		require_once(dirname(__FILE__) . '/../emp/Leader.class.php');
		$aa = new Leader();
		
		return $aa->_list($data['emp']);
	}

	function _notice_2($controller, $request, $data) {
		$from = $data['emp'];

		$subject = sprintf('%s %s', $data['code'], $this->status[$data['status']]);
		$to = $data['bj'];

		$body = $controller->sunxy($request, 'customer/quote/notice_2');
		debug($body);
		
		$this->_notice($from, $to, $subject, $body, 1);
	}

	function _notice_3($controller, $request, $data) {
		$from = $data['bj'];

		$to = $data['emp'];

		$subject = sprintf('%s %s', $data['code'], $this->status[$data['status']]);
		$body = $controller->sunxy($request, 'customer/quote/notice_3');
		debug($body);
		$this->_notice($from, $to, $subject, $body, 1);
	}

	function _notice_8($controller, $request, $data) {
		$from = $data['bj'];

		$to = $data['emp'];

		$subject = sprintf('%s %s', $data['code'], $this->status[$data['status']]);
		// debug($subject);

		$body = $controller->sunxy($request, 'customer/quote/notice_8');
		debug($body);
		$this->_notice($from, $to, $subject, $body, 1);
	}

	function _tj_a($controller, $request) {
		$s = $this->groupby($controller, $this->groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$fmt = "SELECT %s as a, SUM(num) as c FROM %s a WHERE createtime > %s GROUP BY %s ORDER BY c DESC";
		$sql = sprintf($fmt, $s, $this->tbl, $df, $s);
		echo $sql . "\n";

		$this->_group_tj($controller, $sql);

		return $controller->sunxy($request, 'tj_a');
	}
	
	function move_emp($from, $to) {
		require_once(dirname(__FILE__) . '/../customer/QuoteInfo.class.php');
		$info = new QuoteInfo();
		
		$info->move_emp($from, $to);
		// $info->move_emp($from, $to);
		
		$db = newdb($this->dsn_w);		
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $from);
		debug($sql);
		$db->query($sql);
	}
	
	function move_id_emp($customer, $to) {		
		require_once(dirname(__FILE__) . '/../customer/QuoteInfo.class.php');
		$info = new QuoteInfo();
		
		$db = newdb($this->dsn_w);		
		$fmt = "SELECT * FROM %s WHERE customer = '%s'";
		$sql = sprintf($fmt, $this->tbl, $customer);
		debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET emp = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $to, $row['id']);
			debug($sql);
			$db->query($sql);
			
			$info->move_id_emp($row['id'], $to);
		}
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email('316');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '报价信息';
				
		$cc = array(
			'完成报价'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND status = 8", 'b'=>'status'),	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}
		
		$controller->clear_assign('result');

		$body = $this->_tj_html($controller, $request, 'customer/quote/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjbj($controller, $request) {
		$bb = $this->_list_email('205');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '报价员情况';
		
		$cc = array();
		$a = $this->emp_dept('scbj');
		while (list($k, $v) = each($a)) {
			if ($k == '-') {
				continue;
			}
			
			$w = sprintf("bj = '%s' AND status < 8 AND createtime > DATE_ADD(now(), INTERVAL -15 DAY)", $k);
			$cc[$v] = array('w'=>$w, 'b'=>'status');
		}
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}

		$body = $this->_tj_html($controller, $request, 'customer/quote/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjw($controller, $request) {
		$bb = $this->_list_email('316');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->pretreat($controller);

		$from = $this->email;
		$subject = '报价信息（周报）';
				
		$cc = array(
			'完成报价'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND status = 8", 'b'=>'status'),	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);

		$body = $this->_tj_html($controller, $request, 'customer/quote/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'emp';
			
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, SUM(num) AS c FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, SUM(num) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}		
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function _list_3($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE customer = '%s' ORDER BY id DESC LIMIT 8";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$ret[] = $row;
		}
		
		return $ret;
	}
	
	function _exist($customer) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE customer = '%s'";
		$sql = sprintf($fmt, $this->tbl, $customer);
		echo $sql . "\n";
		$result = $db->query($sql);		
		if ($db->num_rows($result) > 0) {
			return true;
		}
		
		return false;		
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'customer/quote/_list');
		return true;
	}
	
	function view_customer($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_customer($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'customer/quote/_list_c');
		return true;
	}
}
?>